import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:baseProjectFlutter/utils/common/assets_utils.dart';

import '../../config/theme.dart';

/// SVG图标组件 封装夜间模式
class SvgWidget extends StatelessWidget {
  final String url; // SVG图标的路径,可以本地，可以网络
  final double? width; // 图标的宽度
  final double? height; // 图标的高度
  final BoxFit fit;
  final ColorFilter? colorFilter; // 颜色过滤器，可以设置图标颜色
  final themeVm = Get.put(ThemeVm());

  SvgWidget(
    this.url, {
    this.width,
    this.height,
    this.fit = BoxFit.contain,
    this.colorFilter,
  });

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      var isLocal = false;
      var urlResult = url;
      if (url.startsWith("assets")) {
        isLocal = true;
        final isDarkMode = themeVm.themeMode == ThemeMode.dark;
        urlResult = isDarkMode ? AssetsUtils.getDarkAssetPath(url) : url;
      } else if (url.startsWith("http://") || url.startsWith("https://")) {
        isLocal = false;
      }
      return isLocal
          ? SvgPicture.asset(
              urlResult,
              width: width,
              height: height,
              fit: fit,
              colorFilter: colorFilter,
            )
          : SvgPicture.network(
              urlResult,
              width: width,
              height: height,
              fit: fit,
              colorFilter: colorFilter,
            );
    });
  }
}
